package com.weiweiqin.controller;

import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weiweiqin.model.Category;
import com.weiweiqin.model.Goods;
import com.weiweiqin.model.GoodsCategoryItem;
import com.weiweiqin.model.Pic;
import com.weiweiqin.service.CategoryService;
import com.weiweiqin.service.GoodsService;
import com.weiweiqin.vo.common.GoodsTabVO;
import com.weiweiqin.vo.common.GoodsVO;
import com.weiweiqin.vo.common.Page;
import com.weiweiqin.vo.common.Result;

@Controller
@RequestMapping("/goods")
public class GoodsController {

	private static final Logger logger = Logger
			.getLogger(GoodsController.class);
	private GoodsService goodsService;
	
	private CategoryService categoryService;
	

	@RequestMapping(value = "/add.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String add(GoodsVO goodsVO) {
		Result result = new Result();
		Goods goods = goodsVO.getGoods();
		goodsService.save(goods);
		List<Pic> picList = goodsVO.getPicList();
		if(null != picList && !picList.isEmpty()){
			for (Pic pic : picList) {
				if(!StringUtils.isEmpty(pic.getName())){
					goodsService.savePic(pic);;
				}
			}
		}
		
		Category category = goodsVO.getCategory();
		GoodsCategoryItem goodsCategoryItem = new GoodsCategoryItem();
		if(null != category){
			goodsCategoryItem.setCategoryId(category.getId());
			goodsCategoryItem.setGoodsId(goods.getId());
			goodsService.saveGoodsCategoryItem(goodsCategoryItem);
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/del.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String del(int id)  {
		Result result = new Result();
		try {
			goodsService.del(id);
		} catch (Exception e) {
			logger.error("数据库异常...",e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	
	@RequestMapping(value = "/get.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String get(int id)  {
		Result result = new Result();
		try {
			Goods goods = goodsService.get(id);
		} catch (Exception e) {
			logger.error("数据库异常...",e);
			result.setResult(false);
			result.setErrorMsg("数据库异常。。。");
			return result.toJson();
		}
		result.setResult(true);
		return result.toJson();
	}
	

	@RequestMapping(value = "/pageList.do", method = { RequestMethod.POST,
			RequestMethod.GET })
	@ResponseBody
	public String pageList(int currPage, HttpServletResponse response)
			throws UnsupportedEncodingException {
		Result result = new Result();
		Page page = new Page();
		page.setCurrPage(currPage);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		List<Goods> goodsList = goodsService.pageList(page, null);
		List<GoodsTabVO> voList = new ArrayList<GoodsTabVO>();
		if(null != goodsList && !goodsList.isEmpty()){
			for (Goods goods : goodsList) {
				GoodsTabVO goodsTabVO = new GoodsTabVO();
				goodsTabVO.setGoods(goods);
				goodsTabVO.setUpdateDate(sdf.format(goods.getUpdateDate()));
				voList.add(goodsTabVO);
			}
			
		}
	
		
		int totalCount = goodsService.totalCount();
		page.setTotalCount(totalCount);
		Map<String,Object> mp = new HashMap<String,Object>();
		mp.put("totalPage",page.getTotalPage());
		mp.put("goodsList", voList);
		result.setObj(mp);
		result.setResult(true);
		return result.toJson();
	}

	public GoodsService getGoodsService() {
		return goodsService;
	}

	public void setGoodsService(GoodsService goodsService) {
		this.goodsService = goodsService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}
}
